if (typeof axios === 'undefined') {
    throw new Error('请先引入axios');
}
/* axios的全局配置 */
axios.defaults.baseURL = 'https://music.hzbiz.net';
// axios.defaults.headers.common['SAT'] = '1234567';
// axios.defaults.headers.common['TOKEN'] = localStorage.getItem('token');
axios.defaults.headers.post['Content-Type'] = 'multipart/form-data';

// 获取元素，DOM操作，toast弹窗功能
const getEl = {
    _(el) {
        return document.querySelector(el);
    },
    _all(el) {
        return document.querySelectorAll(el);
    },
    /* ifrom 页面跳转 */
    ifromTo(el, url) {
        this._(el).addEventListener('click', () => {
            console.log(this._('#Ifrom'));
            console.log(url);
            this._('#Ifrom').src = url;
        })
    },
    //成功toast弹窗
    toast_success(value = '操作成功！',box = '.toast_success', callback) {
        let div = document.querySelector(box);
        let P = document.querySelector(box + ' p');
        // console.log(P);
        // console.log(value);
        P.innerText = value;
        div.classList.remove('fadeOutUp');
        div.classList.add('animated');
        div.classList.add('fadeInDown');

        setTimeout(() => {
            div.classList.remove('fadeInDown');
            div.classList.add('fadeOutUp');
            callback && callback();
        }, 3000);
        div.addEventListener('click', function () {
            div.classList.remove('fadeInDown');
            div.classList.add('fadeOutUp');
            callback && callback();
            // alert(00)
        });
    },
    //失败的toast弹窗
    toast_error(value = '操作失败！',box = '.toast_error', callback,msg) {
        let div = document.querySelector(box);
        let P = document.querySelector(box + ' p');
        // console.log(P);
        // console.log(value);
        P.innerText = value;
        div.classList.remove('fadeOutUp');
        div.classList.add('animated');
        div.classList.add('fadeInDown');
        div.setAttribute('title',msg)
        setTimeout(() => {
            div.classList.remove('fadeInDown');
            div.classList.add('fadeOutUp');
            callback && callback();
        }, 3000);
        div.addEventListener('click', function () {
            div.classList.remove('fadeInDown');
            div.classList.add('fadeOutUp');
            callback && callback();
            // alert(11)
        });
    },
    // 角色列表获取复选框被选中值的方法
     checkboxValue(arr) {
        for (let i = 0; i < getEl._all('.checkbox input').length; i++) {
            const el = getEl._all('.checkbox input')[i];
            if (el.checked) {
                console.log(el.value);
                arr.push(el.value);
            }
        }
        return arr;
    },
    // 获取单选框的值
     getRadioValue(radioName) {
        let radioValue;
        radioValue = document.getElementsByName(radioName);
        if (radioValue != null) {
            for (let i = 0; i < radioValue.length; i++) {
                if (radioValue[i].checked) {
                    return radioValue[i].value;
                }
            }
        }
        return null;
    }

}
const personal = {
    phone(data){
        return axios({
            method:'post',
            url:'/login/cellphone',
            data
        }).then(res=>{
            console.log(res);
            let {
                verify,
                token,
                user
            } = res.data;
            if (verify) {
                localStorage.setItem('token', token);
                localStorage.setItem('user', JSON.stringify(user));
            }
            return Promise.resolve(res)
        })
    }
} 

/* 输入总毫秒数，返回日期对象 */
function formatDate(shijianchuo) {
    //补零函数
    function add0(m) {
        return m < 10 ? '0' + m : m
    }
    var time = new Date(shijianchuo);
    var y = time.getFullYear();
    var m = time.getMonth() + 1;
    var d = time.getDate();
    var h = time.getHours();
    var mm = time.getMinutes();
    var s = time.getSeconds();
    return y + '-' + add0(m) + '-' + add0(d) + ' ' + add0(h) + ':' + add0(mm) + ':' + add0(s);
}
